package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.BaseGrade;
import com.yuyou.fn.educational.mapper.BaseGradeMapper;
import com.yuyou.fn.educational.service.IBaseGradeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 *
 * BaseGrade 表数据服务层接口实现类
 *
 */
@Service
public class BaseGradeServiceImpl extends SuperServiceImpl<BaseGradeMapper, BaseGrade,Long> implements IBaseGradeService {


    @Cacheable("CacheOfGradeList")
    @Override
    public List<BaseGrade> findAllFromCache() {
        Example<BaseGrade> example = BaseGrade.createExample();
        example.orderBy("gradeId").desc();
        return findListByExample(example);
    }

    @Override
    public List<String> findGradeNameByIds(List<Long> gradeIds) {
        Example<BaseGrade> example = BaseGrade.createExample();
        example.selectProperties("gradeName");
        example.createCriteria().andIn("gradeId", gradeIds );
        List<BaseGrade> gradeList = findListByExample(example);
        return BeanUtils.toList(gradeList,"gradeName");
    }

    @Override
    public String findGradeNameById(Long gradeId) {

        Example<BaseGrade> example = BaseGrade.createExample();
        example.selectProperties("gradeName");
        example.createCriteria().andEqualTo("gradeId",gradeId);
        BaseGrade baseGrade = findOneByExample(example);

        return baseGrade == null ? StringUtils.EMPTY : baseGrade.getGradeName() ;
    }

    @Override
    public BaseGrade findGradeByGradeName(String gradeName) {
        Example<BaseGrade> example = BaseGrade.createExample();
        example.createCriteria().andEqualTo("gradeName",gradeName);
        return findOneByExample(example);
    }

    @Override
    public List<BaseGrade> findGradeByGradeNames(List<String> gradeNames) {
        if (Utils.checkListEmpty(gradeNames)){
            return Collections.EMPTY_LIST;
        }
        Example<BaseGrade> example = BaseGrade.createExample();
        example.createCriteria().andIn("gradeName",gradeNames);
        return findListByExample(example);
    }

    @Override
    public List<BaseGrade> findDealOtherPosition(List<Long> gradeIds) {
        if (gradeIds==null || gradeIds.size()<=0){
            return Collections.EMPTY_LIST;
        }
        Example<BaseGrade> example = BaseGrade.createExample();
        example.orderBy("gradeId").desc();
        example.createCriteria().andIn("gradeId",gradeIds);
        List<BaseGrade> baseGradeList=this.findListByExample(example);
        Iterator<BaseGrade> baseGradeIterator=baseGradeList.iterator();
        while (baseGradeIterator.hasNext()){
            BaseGrade baseGrade=baseGradeIterator.next();
            if (baseGrade.getGradeId().equals(99L)){
                baseGradeIterator.remove();
                baseGradeList.add(baseGrade);
                break;
            }
        }
        return baseGradeList;
    }

    @Override
    @Cacheable("CacheOfOrderGradeList")
    public List<BaseGrade> findAllOrderGrade() {
        List<BaseGrade> baseGradeList=this.findAllFromCache();
        Iterator<BaseGrade> baseGradeIterator=baseGradeList.iterator();
        while (baseGradeIterator.hasNext()){
            BaseGrade baseGrade=baseGradeIterator.next();
            if (baseGrade.getGradeId().equals(99L)){
                baseGradeIterator.remove();
                baseGradeList.add(baseGrade);
                break;
            }
        }
        return baseGradeList;
    }

    @Override
    public Map<Long, BaseGrade> findGradeMap() {
        List<BaseGrade> baseGradeList = this.findAllFromCache();
        return BeanUtils.toMap(baseGradeList,"gradeId");
    }
}